package com.platform.comm.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;


/**
 * 图片文件base64转换工具
 *
 * @author wxw
 * @version 1.0
 * @date 2021-09-16
 */

public class Base64ImageUtils {
    /**
     * 将网络图片进行Base64位编码
     * 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     */
    public static String encodeImgageToBase64(URL imageUrl) {
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageUrl);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        assert outputStream != null;
        return encoder.encode(outputStream.toByteArray());
    }

    /**
     * 将图片流进行Base64位编码
     * 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     */
    public static String encodeImageToBase64(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        RestUtil.copy(inputStream, outputStream);
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outputStream.toByteArray());
    }

    /**
     * 将本地图片进行Base64位编码
     * 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
     */
    public static String encodeImageToBase64(File imageFile) {
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outputStream.toByteArray());
    }

    /**
     * 将Base64位编码的图片进行解码，并保存到指定文件夹
     *
     * @param base64 base64编码的图片信息
     */
    public static void decodeBase64ToImage(String base64, String path,
                                           String imgName) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            FileOutputStream write = new FileOutputStream(new File(path
                    + imgName));
            byte[] decoderBytes = decoder.decodeBuffer(base64);
            write.write(decoderBytes);
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static InputStream decodeBase64ToImage(String base64) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return new ByteArrayInputStream(decoder.decodeBuffer(base64));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL("http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/9.jpg");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        String encoderStr = Base64ImageUtils.encodeImgageToBase64(url);
        System.out.println(encoderStr);

        Base64ImageUtils.decodeBase64ToImage(encoderStr, "E:/", "football.jpg");

    }
}
